package com.yc.AdminUI;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FillLayout;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.UI.BuyingTicket;
import com.yc.dao.DBHelper;
import com.yc.dao.TrainDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Combo;

/**
 * 票务管理
 * @author UNO
 * 2021年6月24日下午7:03:14
 */
public class TicketingControl extends Composite {
	private Text text_1;
	private Text text_2;
	private Table table;
	private Text text_3;
	private Text text_5;
	private Text text_7;
	private Text text_4;
	private Text text_6;
	private Text text_8;
	String cname = null;
	String date = null;//用户选择的出发日期
	private Map<String, Object> amap = null;//用户点击的那一趟列车的票价信息
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public TicketingControl(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite_1 = new Composite(this, SWT.NONE);
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		Label lblNewLabel = new Label(composite_1, SWT.NONE);
		lblNewLabel.setText("到");
		lblNewLabel.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.BOLD));
		lblNewLabel.setAlignment(SWT.CENTER);
		lblNewLabel.setBounds(189, 12, 45, 26);

		Label lblNewLabel_2 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2.setBounds(58, 330, 62, 38);
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		lblNewLabel_2.setText("软卧");

		Label lblNewLabel_2_1 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_1.setBounds(126, 330, 122, 38);
		lblNewLabel_2_1.setText("余 票 数  ：");
		lblNewLabel_2_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));

		text_1 = new Text(composite_1, SWT.BORDER);
		text_1.setBounds(254, 335, 122, 28);

		Label lblNewLabel_2_2 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_2.setBounds(432, 330, 83, 38);
		lblNewLabel_2_2.setText("价格  ：");
		lblNewLabel_2_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));

		text_2 = new Text(composite_1, SWT.BORDER);
		text_2.setBounds(521, 335, 122, 28);

		Label lblNewLabel_2_3 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_3.setText("硬卧");
		lblNewLabel_2_3.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		lblNewLabel_2_3.setBounds(58, 380, 62, 38);

		Label lblNewLabel_2_4 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_4.setText("硬座");
		lblNewLabel_2_4.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		lblNewLabel_2_4.setBounds(58, 430, 62, 38);

		Label lblNewLabel_2_5 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_5.setText("无座");
		lblNewLabel_2_5.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		lblNewLabel_2_5.setBounds(58, 480, 62, 38);

		table = new Table(composite_1, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setBounds(0, 59, 933, 249);

		TableColumn tableColumn_1 = new TableColumn(table, SWT.CENTER);
		tableColumn_1.setWidth(85);
		tableColumn_1.setText("列车序号");

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn.setWidth(150);
		tblclmnNewColumn.setText("列车类型");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_1.setWidth(85);
		tblclmnNewColumn_1.setText("列车名");

		TableColumn tableColumn_2 = new TableColumn(table, SWT.CENTER);
		tableColumn_2.setWidth(80);
		tableColumn_2.setText("列车状态");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("出发站");

		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText("终点站");

		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_4.setWidth(100);
		tblclmnNewColumn_4.setText("出发时间");

		TableColumn tableColumn = new TableColumn(table, SWT.CENTER);
		tableColumn.setWidth(100);
		tableColumn.setText("到站时间");

		TableCursor tableCursor = new TableCursor(table, SWT.NONE);

		Label lblNewLabel_2_1_1 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_1_1.setText("余 票 数  ：");
		lblNewLabel_2_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		lblNewLabel_2_1_1.setBounds(126, 380, 122, 38);

		Label lblNewLabel_2_1_1_1 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_1_1_1.setText("余 票 数  ：");
		lblNewLabel_2_1_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		lblNewLabel_2_1_1_1.setBounds(126, 430, 122, 38);

		Label lblNewLabel_2_1_1_2 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_1_1_2.setText("余 票 数  ：");
		lblNewLabel_2_1_1_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		lblNewLabel_2_1_1_2.setBounds(126, 480, 122, 38);

		text_3 = new Text(composite_1, SWT.BORDER);
		text_3.setBounds(254, 385, 122, 28);

		text_5 = new Text(composite_1, SWT.BORDER);
		text_5.setBounds(254, 435, 122, 28);

		text_7 = new Text(composite_1, SWT.BORDER);
		text_7.setBounds(254, 485, 122, 28);

		Label lblNewLabel_2_2_1 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_2_1.setText("价格  ：");
		lblNewLabel_2_2_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		lblNewLabel_2_2_1.setBounds(432, 380, 83, 38);

		Label lblNewLabel_2_2_2 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_2_2.setText("价格  ：");
		lblNewLabel_2_2_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		lblNewLabel_2_2_2.setBounds(432, 430, 83, 38);

		Label lblNewLabel_2_2_3 = new Label(composite_1, SWT.NONE);
		lblNewLabel_2_2_3.setText("价格  ：");
		lblNewLabel_2_2_3.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		lblNewLabel_2_2_3.setBounds(432, 480, 83, 38);

		text_4 = new Text(composite_1, SWT.BORDER);
		text_4.setBounds(521, 385, 122, 28);

		text_6 = new Text(composite_1, SWT.BORDER);
		text_6.setBounds(521, 435, 122, 28);

		text_8 = new Text(composite_1, SWT.BORDER);
		text_8.setBounds(521, 485, 122, 28);

		Button btnUpdate = new Button(composite_1, SWT.NONE);
		
		btnUpdate.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		btnUpdate.setBounds(702, 400, 108, 50);
		btnUpdate.setText("确认修改");

		Combo combo = new Combo(composite_1, SWT.NONE);
		combo.setItems(new String[] {"衡阳", "长沙"});
		combo.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		combo.setBounds(42, 8, 98, 35);

		Combo combo_1 = new Combo(composite_1, SWT.NONE);
		combo_1.setItems(new String[] {"衡阳", "长沙", "广州", "北京"});
		combo_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		combo_1.setBounds(278, 8, 98, 35);

		DateTime dateTime = new DateTime(composite_1, SWT.BORDER);
		
		dateTime.setBounds(422, 12, 159, 35);

		Label label_1_1 = new Label(composite_1, SWT.NONE);
		label_1_1.setText("查询");
		label_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label_1_1.setBounds(716, 9, 58, 35);

		//表格游标，点击展示票价
		tableCursor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				TrainDao trainDao = new TrainDao();

				cname = ti.getText(2); //获取第三列的值，即列车名

				String year = String.valueOf(dateTime.getYear());
				String month = String.valueOf(dateTime.getMonth() + 1);
				String day = String.valueOf(dateTime.getDay());
				if(month.length() < 2) {
					month = "0" + month;
				}
				if(day.length() < 2) {
					day = "0" + day;
				}
				date = year +"/" + month + "/" + day; //选择的出发日期
				//用户点击的那一趟列车的票价信息
				amap = trainDao.findPrice(cname, date);
				//查查看有没有那一天的票务表，如果没有，就不用管，要修改直接在下方输入后，插入数据
				if(amap == null) {
					//生成用户点击的那一趟列车的票务表
					text_1.setText("");
					text_2.setText("");
					text_3.setText("");
					text_4.setText("");
					text_5.setText("");
					text_6.setText("");
					text_7.setText("");
					text_8.setText("");
					return;
				}
				text_1.setText(amap.get("p_rw").toString());
				text_2.setText(amap.get("p_rwprice").toString());
				text_3.setText(amap.get("p_yw").toString());
				text_4.setText(amap.get("p_ywprice").toString());
				text_5.setText(amap.get("p_yz").toString());
				text_6.setText(amap.get("p_yzprice").toString());
				text_7.setText(amap.get("p_wz").toString());
				text_8.setText(amap.get("p_wzprice").toString());

			}
		});
		//点击修改票务信息
		btnUpdate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				DBHelper db = new DBHelper();
//insert into tb_price values(seq_price_id.nextval, 'G5116','2021/07/02', 100, 175, 100, 167, 100, 75, 100, 75);
				String year = String.valueOf(dateTime.getYear());
				String month = String.valueOf(dateTime.getMonth() + 1);
				String day = String.valueOf(dateTime.getDay());
				if(month.length() < 2) {
					month = "0" + month;
				}
				if(day.length() < 2) {
					day = "0" + day;
				}
				date = year +"/" + month + "/" + day; //选择的出发日期
				int num1 = Integer.valueOf(text_1.getText());
				int pri2 = Integer.valueOf(text_2.getText());
				int num3 = Integer.valueOf(text_3.getText());
				int pri4 = Integer.valueOf(text_4.getText());
				int num5 = Integer.valueOf(text_5.getText());
				int pri6 = Integer.valueOf(text_6.getText());
				int num7 = Integer.valueOf(text_7.getText());
				int pri8 = Integer.valueOf(text_8.getText());
				if(amap == null) {//如果咩有这趟车这一天的票务信息
					//生成用户点击的那一趟列车的票务表
					String sql = "insert into tb_price values(seq_price_id.nextval, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
					int i = db.update(sql, cname, date, num1, pri2, num3, pri4, num5, pri6, num7, pri8);
					if(i > 0) {
						SWTUtil.showMessage(getShell(), "提示", "修改成功");
					}else {
						SWTUtil.showMessage(getShell(), "提示", "修改失败");
					}
					return;
				}
				String sql = "update tb_price set " + 
						"p_rw = ?, p_rwprice = ?,p_yw = ?, p_ywprice = ?,p_yz = ?, p_yzprice = ?,p_wz = ?, p_wzprice = ? " + 
						"where c_name = ? and p_date = ? ";
				int i = db.update(sql, num1, pri2, num3, pri4, num5, pri6, num7, pri8, cname, date);
				if(i > 0) {
					SWTUtil.showMessage(getShell(), "提示", "修改成功");
				}else {
					SWTUtil.showMessage(getShell(), "提示", "修改失败");
				}
				return;
			}
		});

		showAllTrain();
		//点击查询生成查询信息
		label_1_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				TrainDao trainDao = new TrainDao();
				String startstation = combo.getText().trim();
				String destination = combo_1.getText().trim();
				if(startstation == null && destination == null) {
					return;
				}
				List<Map<String, String>> list = trainDao.findUsefulTrain(startstation, destination);//查询适合列车的基本信息
				table.removeAll(); //移除表格中原有的数据
				if (list == null || list.isEmpty()) {
					SWTUtil.showMessage(getShell(), "提示", "暂无相关站点");
					return;
				}
				TableItem ti = null; //行对象
				for(Map<String, String> map : list) {
					ti = new TableItem(table, SWT.NONE);
					ti.setData("c_status", map.get("c_status"));
					if("0".equals(map.get("c_status"))) {
						ti.setText(new String[] {map.get("c_id"), map.get("c_type"),map.get("c_name"),"正常", map.get("c_startstation"),map.get("c_destination"),map.get("c_starttime"),map.get("c_endtime")});
					}else {
						ti.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						ti.setText(new String[] {map.get("c_id"), map.get("c_type"),map.get("c_name"),"停运", map.get("c_startstation"),map.get("c_destination"),map.get("c_starttime"),map.get("c_endtime")});
					}

				}
			}
		});

	}
	/**
	 * 从数据库查询到所有列车，并显示
	 */
	public void showAllTrain() {
		TrainDao trainDao = new TrainDao();
		List<Map<String, String>> list = trainDao.findAllTrain();//查询所有列车的基本信息
		//将所有列车显示到表格中
		table.removeAll(); //移除表格中原有的数据
		if (list == null || list.isEmpty()) {
			return;
		}
		TableItem ti = null; //行对象
		for(Map<String, String> map : list) {
			ti = new TableItem(table, SWT.NONE);
			ti.setData("c_status", map.get("c_status"));
			if("0".equals(map.get("c_status"))) {
				ti.setText(new String[] {map.get("c_id"), map.get("c_type"),map.get("c_name"),"正常", map.get("c_startstation"),map.get("c_destination"),map.get("c_starttime"),map.get("c_endtime")});
			}else {
				ti.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
				ti.setText(new String[] {map.get("c_id"), map.get("c_type"),map.get("c_name"),"停运", map.get("c_startstation"),map.get("c_destination"),map.get("c_starttime"),map.get("c_endtime")});
			}

		}

	}
	

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
